package com.gnetek.netty.thread;

import cn.hutool.core.date.DateUtil;
import cn.hutool.core.util.RandomUtil;
import com.gnetek.netty.client.EchoClient;
import com.gnetek.netty.contact.SequenceFactory;
import com.gnetek.netty.interf.NettyController;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import java.util.Date;
import java.util.List;

/**
 * @Description 数据上传线程
 * @Author Darren Huang
 * @Date 2023-09-08 16:07
 */
public class DataTransRunner implements Runnable{
    private Logger log = LoggerFactory.getLogger(DataTransRunner.class);
    private Integer min;
    private String swarmStr;
    private String honeybeeStr;
    private NettyController nettyCtrl;

    private int count = 1;

    private static Integer[] sum = {1,1,1,1,1,1,1,1};

    public DataTransRunner(String swarmStr, String honeybeeStr, Integer min, NettyController nettyCtrl){
        this.swarmStr = swarmStr;
        this.honeybeeStr = honeybeeStr;
        this.min = min;
        this.nettyCtrl = nettyCtrl;
    }

    private static boolean run;

    public static void doStart(String swarmStr, String honeybeeStr, Integer min, NettyController nettyCtrl){
        run = true;
        new Thread(new DataTransRunner(swarmStr, honeybeeStr, min, nettyCtrl)).start();
    }
    public static void doStop(){run = false;}

    @Override
    public void run() {
        while (run){
            if(EchoClient.getInstance()==null || !EchoClient.getInstance().isConnent()) {
                run=false;
            }else {
                String[] swarms = swarmStr.split(";");
                String[] honeybeeArrays = honeybeeStr.split("\n");

                for(int i=0; i< sum.length; i++){
                    sum[i] = sum[i]+ RandomUtil.randomInt(1, 5);
                }

                for(int i=0; i<swarms.length; i++){
                    String swarmId = swarms[i];

                    String seq = String.format("%08x", SequenceFactory.nextSequence());
                    String cmd = "AAAA" //包头
                            +"xxxx" //整个数据包长度
                            +seq //包序号
                            +"01" //设备类型
                            +"0002"; //命令码

                    //日期 时间
                    String now = DateUtil.formatDateTime(new Date());
                    String[] dateTime = now.split(" ");
                    String[] date = dateTime[0].split("-");
                    String[] time = dateTime[1].split(":");

                    String year = String.format("%06x", Integer.parseInt(date[0]));
                    String month = String.format("%02x", Integer.parseInt(date[1]));
                    String day = String.format("%02x", Integer.parseInt(date[2]));
                    String hour = String.format("%02x", Integer.parseInt(time[0]));
                    String minute = String.format("%02x", Integer.parseInt(time[1]));
                    String second = String.format("%02x", Integer.parseInt(time[2]));

                    cmd += year+month+day+hour+minute+second;

                    String[] honeybeeIds = honeybeeArrays[i].split(";");
                    String rs485 = String.format("%02x", i+1);
                    String honeybeeArrayNum = String.format("%04x", honeybeeIds.length);
                    log.error(honeybeeArrayNum);

                    cmd += "0000000000000000"//总发电量
                            +"0000000000000000"//日发电量
                            +"02"//版本标识
                            +"00000000000000000000000000000000000000000000000000000000000000"//预留31字节
                            +swarmId //采集器ID
                            +"00" //采集器状态
                            +"5630325F475F32303232313130395F31315F5430320A000000" //采集器软件版本号
                            +rs485//采集器 RS485 地址
                            +"0000"//采集器数据预留
                            +honeybeeArrayNum //在线优化器数量
                            +honeybeeArrayNum; //优化器数量

                    for(String honeybeeId : honeybeeIds){
                        //电压小于100， 电流小于30， 功率小于1000
                        cmd += honeybeeId //优化器 MAC 地址
                                +String.format("%04x", count) //计数
                                +rs485 //子网地址
                                +"00" //设备地址
                                +"00" //无线信号强度
                                +String.format("%04x", RandomUtil.randomInt(10, 100))//输入电压1
                                +String.format("%04x", RandomUtil.randomInt(10, 100))//输入电压2
                                +String.format("%04x", RandomUtil.randomInt(10, 30))//输入电流 1
                                +String.format("%04x", RandomUtil.randomInt(10, 30))//输入电流 2
                                +String.format("%04x", RandomUtil.randomInt(10, 100))//输出电压 1
                                +String.format("%04x", RandomUtil.randomInt(10, 100))//输出电压 2
                                +String.format("%04x", RandomUtil.randomInt(10, 1000))//输入功率 1
                                +String.format("%04x", RandomUtil.randomInt(10, 1000))//输入功率 2
                                +RandomUtil.randomInt(46, 70)//温度
                                +"64"//状态
                                +String.format("%08X", RandomUtil.randomInt(1, 32769)) //"00000001" //告警
                                +"0000000000";//预留
                        for(Integer s : sum){
                            //总发电量1 日发电量1 总发电量2 总发电量2 总发电量3 总发电量3 总发电量4 总发电量4
                            cmd+=String.format("%08x", s);
                        }
                        cmd+="0000000000000000000000000000000000";//预留
                    }

                    cmd+="FFFF";
                    cmd = cmd.replaceFirst("xxxx", String.format("%04x", cmd.length()/2));
                    EchoClient.getInstance().sendHexCmd(cmd);
                    nettyCtrl.showMsg(cmd, true);
                    log.info("swarmId采集器发送成功：{}。", cmd);

                    try {
                        Thread.sleep(RandomUtil.randomInt(1000, 5000));
                    } catch (InterruptedException e) {
                        e.printStackTrace();
                    }
                }

                try {
                    Thread.sleep(min * 60000);
                    count++;
                    if(count>4095) count=1;
                } catch (InterruptedException e) {
                    e.printStackTrace();
                }
            }
        }
    }
}
